import requests
from requests import Response
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from yat.test import macro


class BaseOpsApi:
    def __init__(self, token):
        self.headers = None
        self.log = Logger()
        self.comm_sh = CommonSH()
        self.token = token
        self.ip = f"https://{macro.DATAKIT_HOST}:{macro.DATAKIT_PORT}"

    def get_plugins_base_ops_clusterTask_host_list(self, system_os: str = '',
                                                   os_version: str = '', cpu_arch: str = '') -> Response:
        """
        获取已导入服务器的列表
        :param system_os: 操作系统
        :param os_version: 操作系统版本
        :param cpu_arch: cpu架构
        :return: 符合条件的已导入的服务器的列表
        """
        api = f'/plugins/base-ops/clusterTask/host/list?os={system_os}&osVersion={os_version}&cpuArch={cpu_arch}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_plugins_base_ops_cluster_Task_host_user(self, host_id: str) -> Response:
        """
        并行安装获取指定服务器的用户
        :param host_id: 服务器hostId
        :return: 获取用户列表
        """
        api = f'/plugins/base-ops/clusterTask/hostUser/{host_id}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_plugins_base_ops_clusterTask_host(self, ip: str) -> Response:
        """
        获取服务器操作系统信息
        :param ip: 服务器ip
        :return: 服务器信息
        """
        api = f'/plugins/base-ops/clusterTask/host/{ip}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_plugins_base_ops_install_Package_Manager_hasName(self, server_name) -> Response:
        """
        在线下载安装包
        :param server_name: 下载的安装包名称
        :return:
        """
        api = f'/plugins/base-ops/installPackageManager/hasName?name={server_name}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_plugins_base_ops_install_Package_Manager_v2_list_package(self, package_data: dict) -> Response:
        """
        查询安装包是否存在
        :param package_data: 安装包信息
        :return:
        """
        api = f"/plugins/base-ops/installPackageManager/v2/list/package"
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False, json=package_data)
        return response

    def get_plugins_base_ops_cluster_task_node_create(self, batch_cluster_conf: dict) -> Response:
        """
        集群配置
        :param batch_cluster_conf: 集群配置信息
        :return:
        """
        api = f"/plugins/base-ops/cluster/task/node/create"
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False, json=batch_cluster_conf)
        return response

    def get_plugins_base_ops_cluster_Task_create(self, batch_node_conf: dict) -> Response:
        """
        节点配置
        :param batch_node_conf: 节点配置信息
        :return:
        """
        api = f"/plugins/base-ops/clusterTask/create"
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False, json=batch_node_conf)
        return response

    def get_plugins_base_ops_opsCluster_listInstallPackage(self, package_version: str) -> Response:
        """
        获取已上传或已下载的安装包列表
        :param package_version:（极简版：MINIMAL_LIST，）
        :return:
        """
        api = f'/plugins/base-ops/opsCluster/listInstallPackage?version={package_version}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_plugins_base_ops_installPackageManager_list(self, package_version: str) -> Response:
        """
        获取可在线下载安装包列表
        :param package_version:（极简版：MINIMAL_LIST，）
        :return:
        """
        api = f'/plugins/base-ops/installPackageManager/list?packageVersion={package_version}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_plugins_base_ops_installPackageManager_hasName(self, pkg_name: str) -> Response:
        """
        校验安装包名称是否存在
        :param pkg_name: 待校验安装包名称
        :return:
        """
        api = f'/plugins/base-ops/installPackageManager/hasName?name={pkg_name}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def post_plugins_base_ops_installPackageManager(self, data: dict, file_path: str) -> Response:
        """
        上传数据库安装包
        :param data: 安装包版本信息
        :param file_path: 待上传安装包路径
        :return:
        """
        api = '/plugins/base-ops/installPackageManager'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        files = {
            "file": open(file_path, "rb")
        }
        response = requests.post(url=url, data=data, files=files, headers=self.headers, verify=False)
        return response

    def get_plugins_base_ops_installPackageManager_getCpuArch(self, install_package_path: str,
                                                              version: str) -> Response:
        """
        获取安装包的架构
        :param install_package_path:安装包上传路径
        :param version: 安装包版本（极简版：MINIMAL_LIST，）
        :return:
        """
        api = f'/plugins/base-ops/installPackageManager/getCpuArch?installPackagePath={install_package_path}&version={version}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def get_plugins_base_ops_opsCluster_listCluster(self):
        """
        查看集群列表
        :return:
        """
        api = '/plugins/base-ops/opsCluster/listCluster'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response


